package com.rocching.easychat.core;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Toast;

import androidx.activity.result.ActivityResult;
import androidx.activity.result.ActivityResultCallback;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.FragmentActivity;

import com.rocching.easychat.MainActivity;
import com.rocching.easychat.ui.NewFriendActivity;

import io.rong.imkit.conversationlist.ConversationListAdapter;
import io.rong.imkit.conversationlist.model.BaseUiConversation;
import io.rong.imkit.subconversationlist.SubConversationListFragment;
import io.rong.imkit.widget.adapter.BaseAdapter;
import io.rong.imkit.widget.adapter.ViewHolder;
import io.rong.imlib.model.MessageContent;
import io.rong.message.ContactNotificationMessage;

public class SubConversationListFragmentEx extends SubConversationListFragment {
    private final String TAG = SubConversationListFragmentEx.class.getName();
    private ActivityResultLauncher<Intent> launcher;

    public SubConversationListFragmentEx() {

    }

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        initLauncher();
    }

    @Override
    protected ConversationListAdapter onResolveAdapter() {
        return new ConversationListAdapterEx();
    }

    @Override
    public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        mAdapter.setItemClickListener(new BaseAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(View view, ViewHolder holder, int position) {
                BaseUiConversation baseUiConversation = mAdapter.getItem(position);
                MessageContent messageContent = baseUiConversation.mCore.getLatestMessage();
                if (messageContent instanceof ContactNotificationMessage) {
                    skipNewFriendActivity();
                } else {
                    Toast.makeText(getContext(), "暂未实现点击事件", Toast.LENGTH_SHORT).show();
                }
            }

            @Override
            public boolean onItemLongClick(View view, ViewHolder holder, int position) {
                return false;
            }
        });
    }

    private void skipNewFriendActivity() {
        try {
            Intent intent = new Intent(getActivity(), NewFriendActivity.class);
            launcher.launch(intent);
        } catch (Exception e) {
            Log.e(TAG, e.getMessage(), e);
        }
    }

    private void initLauncher() {
        launcher = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), new ActivityResultCallback<ActivityResult>() {
            @Override
            public void onActivityResult(ActivityResult result) {
                int resultCode = result.getResultCode();
                if (resultCode != Activity.RESULT_OK) {
                    return;
                }
                Intent data = result.getData();
                if (data == null) {
                    return;
                }

                boolean needRefresh = data.getBooleanExtra("needRefresh", false);
                Log.i(TAG, "the needRefresh is:" + needRefresh);
                if (needRefresh) {
                    FragmentActivity activity = getActivity();
                    if (activity instanceof SubConversationListActivity) {
                        ((SubConversationListActivity) activity).setNeedRefresh(true);
                    }
                }
            }
        });
    }
}
